package com.ts.oss;

import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

public class Video2Image {
    public static void main(String[] args) {


//                String videoFilePath = "path/to/your/video.mp4"; // 视频文件路径
//                String outputImagePath = "path/to/output/thumbnail.jpg"; // 输出图片路径
//
//                try {
//                    // 使用ffmpeg命令提取视频的第一帧
//                    ProcessBuilder processBuilder = new ProcessBuilder(
//                            "ffmpeg", "-i", videoFilePath, "-ss", "00:00:01.000", "-vframes", "1", outputImagePath
//                    );
//                    Process process = processBuilder.start();
//                    process.waitFor(); // 等待ffmpeg进程执行完成
//
//                    System.out.println("Thumbnail extracted successfully.");
//                } catch (IOException | InterruptedException e) {
//                    e.printStackTrace();
//                    System.out.println("Error extracting thumbnail.");
//                }



             File file=new File("D:/随记/Java项目——谷粒学院/1-阿里云上传测试视频/6 - What If I Want to Move Faster.mp4");





        // 获取要取得的帧数
            final int fifthFrame= 5;

            FFmpegFrameGrabber grabber;
            InputStream img=null ;
                try {
                    FileInputStream is=new FileInputStream(file);
                    grabber = new FFmpegFrameGrabber(is);

                    grabber.start();
                    // 视频总帧数
                    int videoLength = grabber.getLengthInFrames();

                    Frame frame = null;
                    int i = 0;
                    while (i < videoLength) {
                        // 过滤前5帧,因为前5帧可能是全黑的
                        frame = grabber.grabFrame();
                        if ((i > fifthFrame) && (frame.image != null)) {
                            break;
                        }
                        i++;
                    }

                    Java2DFrameConverter converter = new Java2DFrameConverter();
                    // 绘制图片
                    BufferedImage bi = converter.getBufferedImage(frame);

                    System.out.println(bi.getHeight());

                    File outputFile = new File("D:/output.jpg");
                    ImageIO.write(bi, "jpg", outputFile);
//                    img = bufferedImageToInputStream(bi);

                    grabber.stop();
                    grabber.close();


                } catch (IOException e) {
                    e.printStackTrace();
                }

//                return img;

            /**
             * 将BufferedImage转换为InputStream
             * @param image
             * @return
             */
//            public static InputStream bufferedImageToInputStream(BufferedImage image){
//                ByteArrayOutputStream os = new ByteArrayOutputStream();
//                try {
//                    ImageIO.write(image, "png", os);
//                    InputStream input = new ByteArrayInputStream(os.toByteArray());
//                    return input;
//                } catch (IOException e) {
//
//                }
//                return null;
//            }

        }

}
